<?php

/**
 * servicebutton actions.
 *
 * @package    ems1
 * @subpackage servicebutton
 * @author     Jacobo Martínez
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class servicebuttonActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $buttons = $this->getRoute()->getObjects();
  }

  public function executeShow(sfWebRequest $request)
  {
    $button = $this->getRoute()->getObject();

    return $this->renderPartial('show', array('button' => $button));
  }

  public function executeNew(sfWebRequest $request)
  {
    $portal = $this->getRoute()->getObject();

    $button = new EmsServiceButton();

    $button->setPortal($portal);

    $form = new EmsServiceButtonForm($button, array('portal_id' => $portal->getId()));

    return $this->renderPartial('form', array('form' => $form));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $portal = $this->getRoute()->getObject();

    $button = new EmsServiceButton();

    $button->setPortal($portal);

    $form = new EmsServiceButtonForm($button, array('portal_id' => $portal->getId()));

    if(!$this->processForm($request, $form)){
        return $this->renderPartial('form', array('form' => $form));
    } else {
        return sfView::NONE;
    }
  }

  public function executeEdit(sfWebRequest $request)
  {
    $button = $this->getRoute()->getObject();

    $form = new EmsServiceButtonForm($button, array('portal_id' => $button->getPortalId()));

    return $this->renderPartial('form', array('form' => $form));
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $button = $this->getRoute()->getObject();

    $form = new EmsServiceButtonForm($button, array('portal_id' => $button->getPortalId()));

    if(!$this->processForm($request, $form)){
        return $this->renderPartial('form', array('form' => $form));
    } else {
        return sfView::NONE;
    }
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->getRoute()->getObject()->delete();

    $this->redirect('servicebutton/index');
  }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        $valid = $form->isValid();

        if ($valid)
        {
            $audit = new EmsAudit();

            if($form->getObject()->isNew()){
                $audit->setAction(Doctrine::getTable('sfGuardPermission')->findOneByName('Crear boton de servicio'));
            } else {
                $audit->setAction(Doctrine::getTable('sfGuardPermission')->findOneByName('Editar boton de servicio'));
            }

            $button = $form->save();

            $user = $this->getUser()->getGuardUser();

            $audit->setPortal($button->getPortal());
            $audit->setUser($user);
            $audit->setObjectId($button->getId());

            $audit->save();

            return $this->renderPartial('show', array('button' => $button));
        }

        return $valid;
    }

    public function executeMultiupdate(sfWebRequest $request)
    {
        $button = $this->getRoute()->getObject();

        return $this->renderPartial('multiUpdate', array('button' => $button));
    }

    public function executeTempFileUpload(sfWebRequest $request){

        $service_button = $this->getRoute()->getObject();

        $form = new EmsServiceButtonForm($service_button,array('portal_id' => $request->getParameter('portal_id')));

        $files = $request->getFiles($form->getName());

        $json_response = array();

        $file = null;

        $errors = array();

        $thumb_type = $request->getParameter('thumb','');

        if(!(strcasecmp($thumb_type, 'small') == 0) && !(strcasecmp($thumb_type, 'big') == 0)){
            $json_response['valid'] = false;
            $json_response['error_message'] = 'Tipo de thumbnail incorrecto.';
            return $this->renderText(json_encode($json_response));
        }
        
        $file = $files['thumb_'.$thumb_type.'_tmp'];

        $validator = new jsValidatorImage(array(
                            'mime_types' => array('image/jpeg'),
                            'trim'       => true,
                            'path'       => sfConfig::get('sf_upload_dir').'/portal_'.$request->getParameter('portal_id').'/temp_files/',
                            'required'   => false,
                            'max_size'   => '1048576',
                            'minx'       => strcasecmp($thumb_type, 'small') == 0 ? 25 : 50,
                            'maxx'       => strcasecmp($thumb_type, 'small') == 0 ? 25 : 50,
                            'miny'       => strcasecmp($thumb_type, 'small') == 0 ? 25 : 50,
                            'maxy'       => strcasecmp($thumb_type, 'small') == 0 ? 25 : 50
                        ), array(
                            'required'   => 'Debe indicar el archivo de formato .JPEG a ser cargado.',
                            'max_size'   => 'La imagen excede el peso máximo de archivo permitido, 1 MB.',
                            'mime_types' => 'Tipo de archivo invalido (%mime_type%), el archivo a cargar debe ser de tipo JPEG'
                        ));

        try {

            $validator->clean($file);

        } catch (sfValidatorError $e) {

            $json_response['valid'] = false;

            $json_response['error_message'] = $e->getMessage();

            return $this->renderText(json_encode($json_response));
        }

        $json_response['valid'] = true;

        $json_response['file_name'] = $file['name'];

        $json_response['temp_file'] = Util::saveTempFile($file, $request->getParameter('portal_id'));

        $json_response['size'] = $file['size'];

        $json_response['type'] = $file['type'];

        $json_response['portal_id'] = $request->getParameter('portal_id');
        
        return $this->renderText(json_encode($json_response));
    }
}
